const conncetions = require('../app/database');

class CommentService {
  async createComment(momentId, content, userId) {
    const sql = `INSERT INTO comment (moment_id, user_id, content) VALUES (?, ?, ?)`;
    const result = await conncetions.execute(sql, [momentId, userId, content]);
    console.log(result);
    return result[0];
  }

  async reply(commentId, content, userId, momentId) {
    try {
      const sql = `INSERT INTO comment (moment_id, user_id, content, comment_id) VALUES (?, ?, ?, ?)`;
      const result = await conncetions.execute(sql, [momentId, userId, content, commentId]);
      return result[0];
    } catch (e) {
      console.log(e)
    }
  }

  async updateComment(commentId, content) {
    try {
      const sql = `UPDATE comment SET content = ? WHERE id = ?`;
      const result = await conncetions.execute(sql, [content, commentId]);
      return result[0];
    } catch (e) {
      console.log(e)
    }
  }
  async deleteComment(commentId, userId) {
    try {
      const sql = `DELETE FROM comment WHERE id = ? AND user_id = ?`;
      const result = await conncetions.execute(sql, [commentId, userId]);
      return result[0];
    } catch (e) {
      console.log(e)
    }
  }
}

module.exports = new CommentService();